<!--
 * @Descripttion: 角色信息弹窗
 * @Author: chendingfengmail@163.com
 * @version:
 * @Date: 2021-06-10 14:40:56
 * @LastEditors: chendingfengmail@163.com
 * @LastEditTime: 2022-01-10 15:39:50
-->
<template>
  <el-dialog
    :title="titleName"
    top="30vh"
    width="580px"
    center
    :visible.sync="visible"
    :before-close="modelClose"
    @open="modelOpen"
    :close-on-click-modal="false">
    <el-form
      :rules="roleHandleRules"
      ref="roleHandleForm"
      :model="modelData"
      :disabled="formDisabled"
      v-loading="loading">
      <el-form-item label="角色名称" :label-width="formLabelWidth" prop="description">
        <el-input
          v-model.trim="modelData.description"
          maxlength="40"
          autocomplete="off"
          placeholder="请输入中文名称">
        </el-input>
      </el-form-item>
      <el-form-item label="系统名称" :label-width="formLabelWidth" prop="name">
        <el-input
          v-model.trim="modelData.name"
          maxlength="40"
          autocomplete="off"
          placeholder="请输入英文名称">
        </el-input>
      </el-form-item>
      <el-form-item label="角色描述" :label-width="formLabelWidth" prop="reserver1">
        <el-input
          v-model.trim="modelData.reserver1"
          maxlength="100"
          autocomplete="off"
          placeholder="请输入描述信息">
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" v-show="btnVisible">
      <!-- <el-button @click="modelClose">取 消</el-button> -->
      <el-button type="primary" @click="formSubmit" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapActions } from 'vuex';
export default {
  name: 'roleHandleDialog',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    modelType: {
      type: String,
      required: true,
      default: () => {
        return 'info';
      },
    },
    modelData: {
      type: Object,
      default: () => {
        return {
          name: '你好',
          description: '',
          reserver1: '',
        };
      },
    },
  },
  data() {
    return {
      titleName: '详情',
      formLabelWidth: '80px',
      dialogFormVisible: false,
      formDisabled: false,
      btnVisible: true,
      loading: false,

      roleHandleRules: {
        description: this.$Validate.chTxt(true, '请输入角色名称'),
        name: this.$Validate.integrateTxt(true, '请输入系统可识别的唯一英文名称'),
        reserver1: this.$Validate.otherTxt(true, '请输入角色描述'),
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    ...mapActions('ddAdmin/role', ['loadRoles']),
    // 对话框打开
    modelOpen() {
      switch (this.modelType) {
        case 'add':
          this.modelTypeChange('角色添加', false, true);
          break;
        case 'info':
          this.modelTypeChange('角色详情', true, false);
          break;
        case 'edit':
          this.modelTypeChange('角色编辑', false, true);
          break;
        default:
          break;
      }
    },

    // 模式切换
    modelTypeChange(titleName, formDisabled, btnVisible) {
      this.titleName = titleName;
      this.formDisabled = formDisabled;
      this.btnVisible = btnVisible;
    },

    // 表单提交
    formSubmit() {
      this.$refs.roleHandleForm.validate((valid) => {
        if (valid) {
          switch (this.modelType) {
            case 'add':
              this.handleAdd(this.modelData);
              break;
            case 'edit':
              this.handleEdit(this.modelData);
              break;
            default:
              break;
          }
        }
      });
    },

    // 新增
    handleAdd(formData) {
      this.loading = true;
      this.$api.userRole
        .addRole(formData)
        .then((res) => {
          this.$message.success('角色添加成功');
          this.loading = false;
          this.modelClose();
          this.loadRoles(false);
          this.$emit('loadList');
        })
        .catch((err) => {
          console.log(err);
          this.loading = false;
        });
    },
    // 编辑
    handleEdit(formData) {
      this.loading = true;
      this.$api.userRole
        .updateRole(formData)
        .then((res) => {
          this.$message.success('角色更新成功');
          this.loading = false;
          this.modelClose();
          this.loadRoles(false);
          this.$emit('loadList');
        })
        .catch((err) => {
          console.log(err);
          this.loading = false;
        });
    },
    // 重置表单
    resetForm() {
      this.$nextTick(() => {
        this.$refs.roleHandleForm.resetFields();
      });
    },
    // 对话框关闭
    modelClose() {
      this.resetForm();
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
  },
};
</script>
<style lang="scss">
</style>
